package com.banshuibao.bsb_android.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by wangpan on 2017/2/23.
 */

public class SearchKey {
    public static String matcherSearchTitle1(String title, String keyword) {
        String content = title;
        String wordReg = "(?i)" + keyword;//用(?i)来忽略大小写
        StringBuffer sb = new StringBuffer();
        Matcher matcher = Pattern.compile(wordReg).matcher(content);
        while (matcher.find()) {
           //这样保证了原文的大小写没有发生变化
            matcher.appendReplacement(sb, "" + matcher.group() + "");
        }
        matcher.appendTail(sb);
        content = sb.toString();
       //如果匹配和替换都忽略大小写,则可以用以下方法
       //content = content.replaceAll(wordReg,""+keyword+"");
        return content;
    }

    /** 	 * 搜索关键字标红
     * * @param title
     * * @param keyword
     * * @return
     * */
    public static String matcherSearchTitle(String title, String keyword){
        String content = title;
        String wordReg = "(?i)"+keyword;
        //用(?i)来忽略大小写
         StringBuffer sb = new StringBuffer();
         Matcher matcher = Pattern.compile(wordReg).matcher(content);
         while(matcher.find()){
        //这样保证了原文的大小写没有发生变化
         matcher.appendReplacement(sb, "<font color=\'#5ec1fa\'>"+matcher.group()+"</font>");
         }
         matcher.appendTail(sb);
         content = sb.toString();
    //     Log.i("Utils", content);
        // 如果匹配和替换都忽略大小写,则可以用以下方法
      //   content = content.replaceAll(wordReg,"<font color=\'#5ec1fa\'>"+keyword+"</font>");
     //   Log.i("Utils", content);
         return content;
    }
}
